|
|
|
|
|
|
|
Next, you can iterate through this collection of employees and load them into the list box using the code underneath the Iteration 1 command button: |
|
|
|
 |
|
|
|
|
Private Sub cmdIterate1_Click()
Dim intLoop As Integer
lstNames.Clear
For intLoop = 1 To moEmps.Count
' Note, the Item method has
' been set to the Default
lstNames.AddItem moEmps(intLoop).LastName & ", " & _
moEmps(intLoop).FirstName
Next
End Sub |
|
|
|
|
|
|
|
|
You can see that this code loops through the Employees collection from 1 to the Count value of the number of Employee objects in the collection. The Collection class always numbers its members starting with 1 instead of 0, just like arrays in Visual Basic do. Notice that for this code to work, you have set the Item method in the Employees collection to be the default method. This option is set by going into the Tools, Procedure Attributes menu option and choosing the (Default) option from the Procedure ID combo box. (Ive tried to match this Employee collection to what the normal collection does, and the Item method is the default for Collection classes.) |
|
|
|
|
|
|
|
|
Adding the _NewEnum Method |
|
|
|
|
|
|
|
|
Before you can use the For...Each Next construct, you need to add a method to the Employees collection class that returns a special data type called IUnknown. This data type is a COM object thats required to return an object from an iterator operator. Theres also a special method attached to the Collection class thats hidden: Its called _NewEnum. This method is responsible for returning an object from the collection and maintaining which object is the next one to return. Add the following code to your Collection classes to use the For...Each Next construct to iterate through your collections: |
|
|
|
 |
|
|
|
|
Public Function Iterate() As IUnknown
Set Iterate = mcolEmps.[_NewEnum]
End Function |
|
|
|
|
|